#include "ReceiveDeviceData.h"

wxDEFINE_EVENT(EVT_RECEIVEFILE_START,ReceiveNewEvent);
wxDEFINE_EVENT(EVT_RECEIVEFILE_STOP,wxCommandEvent);
wxDEFINE_EVENT(EVT_RECEIVEFILE_NEW,ReceiveNewEvent);

ReveiceDeviceData::ReveiceDeviceData(std::ofstream* _filePtr,wxWindow* _win,long allFrameNum) :
    filePtr(_filePtr),
    win(_win),
    AllFrameNum(allFrameNum)
{
    dynamic_cast<wxEvtHandler*>(win)->QueueEvent(new ReceiveNewEvent(EVT_RECEIVEFILE_START,wxID_ANY,allFrameNum));
}

bool ReveiceDeviceData::operator()(const long FrameNum,const char* data)
{
    if(FrameNum!=frameCount || !filePtr->good())
        return false;
    filePtr->write(data,512);
    frameCount++;
    dynamic_cast<wxEvtHandler*>(win)->QueueEvent(new ReceiveNewEvent(EVT_RECEIVEFILE_NEW,wxID_ANY,frameCount));
    return true;
}

ReveiceDeviceData::~ReveiceDeviceData()
{
    dynamic_cast<wxEvtHandler*>(win)->QueueEvent(new wxCommandEvent(EVT_RECEIVEFILE_STOP,wxID_ANY));
    if(filePtr->is_open())
        filePtr->close();
}